#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# xhs_search_dropdown.py

"""
单一博主主页爬虫
使用Playwright自动获取单一博主主页信息
"""

import os
import sys
import re

# 添加项目根目录到Python路径
current_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.dirname(current_dir))

# 初始化playwright配置
from playwright.sync_api import sync_playwright
from xhs_blogger_info.up_info_get import up_tag_get, get_some_up_data
from config import AUTH_FILE, Config

# 相关变量初始化
blogger_info_dict = {}


def main(html_url = 'https://www.xiaohongshu.com/user/profile/63c3601a0000000026010432?xsec_token=ABhqHsM4tuU2Alvj2g35xhegXGezjyehO_CUnnXl6_-sY=&xsec_source=pc_note'):
    with sync_playwright() as p:
        browser = p.chromium.launch(headless=Config.IF_HEADLESS)
        context = browser.new_context(storage_state=AUTH_FILE if os.path.exists(AUTH_FILE) else None)  
        page = context.new_page()

        # 跳转至落地页
        page.goto(html_url)

        # 博主url
        blogger_info_dict['blogger_url'] = html_url

        # 获取博主名
        blogger_name = page.locator('div.user-name').inner_text()
        blogger_info_dict['blogger_name'] = blogger_name
        
        # 获取博主小红书ID
        blogger_redid = re.sub('小红书号：', '', page.locator('span.user-redId').inner_text())
        blogger_info_dict['blogger_redid'] = blogger_redid

        # 获取博主IP所属地
        blogger_ip = re.sub('IP属地：', '', page.locator('span.user-IP').inner_text())
        blogger_info_dict['blogger_ip'] = blogger_ip

        # 获取博主主页页面介绍
        blogger_desc = page.locator('div.user-desc').inner_text()
        blogger_info_dict['blogger_desc'] = blogger_desc

        # 保存页面源码到文件
        html_content = page.content()

        # 获取博主tag
            # info_tag
            # age_tag
            # location_tag
            # profession_tag
            # college_tag
        up_tag_list = up_tag_get(html_content)
        blogger_tag_gender = up_tag_list[0]
        blogger_info_dict['blogger_tag_gender'] = blogger_tag_gender
        blogger_tag_age = up_tag_list[1]
        blogger_info_dict['blogger_tag_age'] = blogger_tag_age
        blogger_tag_location = up_tag_list[2]
        blogger_info_dict['blogger_tag_location'] = blogger_tag_location
        blogger_tag_profession = up_tag_list[3]
        blogger_info_dict['blogger_tag_profession'] = blogger_tag_profession
        blogger_tag_college = up_tag_list[4]
        blogger_info_dict['blogger_tag_college'] = blogger_tag_college

        # 获取博主关注数    
        blogger_follows_num = get_some_up_data(html_content, '关注')
        blogger_info_dict['blogger_follows_num'] = blogger_follows_num
        
        # 获取博主粉丝数
        blogger_fans_num = get_some_up_data(html_content, '粉丝')
        blogger_info_dict['blogger_fans_num'] = blogger_fans_num

        # 获取博主点赞与收藏数
        blogger_interaction_num = get_some_up_data(html_content, '获赞与收藏')
        blogger_info_dict['blogger_interaction_num'] = blogger_interaction_num

        print(blogger_info_dict)

        # 保存登录状态
        context.storage_state(path=AUTH_FILE)
        browser.close()
    return blogger_info_dict

if __name__ == "__main__":
    main('https://www.xiaohongshu.com/user/profile/5b1a104a4eacab2756796579?xsec_token=AB_gq7bPO6YiXh8frfYJmeuUvMPxSI9FSRohes5qGvRdA=&xsec_source=pc_search')